package com.baidu.springboot_10_simpledemo.controller;

import com.baidu.springboot_10_simpledemo.domain.User;
import com.baidu.springboot_10_simpledemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private UserService service;

    @PostMapping("/login")
    public String login(User user, Model model, HttpSession session){
        User u = service.findUser(user);
        if(u!=null){
            //登录成功
            session.setAttribute("loginUser",u);// 把用户存入session
            return "redirect:/main.html";
        }else{
            //未注册，登录失败
            model.addAttribute("msg","用户名或密码错误");
            return "login";
        }
    }

}
